﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Hotel.WPF.HotelWCFSurIIS;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;
//using log4net;

namespace Hotel.WPF.ui.Client
{
    /// <summary>
    /// Interaction logic for W_Client.xaml
    /// </summary>
    public partial class W_Client : Window
    {

        #region "Variables"

            /// <summary>
            /// Service WCF Client
            /// </summary>
            HotelClient srvService;

            /// <summary>
            /// Log4net
            /// </summary>
            //private log4net.ILog Logger = LogManager.GetLogger("Malaw.Logging");

        #endregion


        public W_Client()
        {
            InitializeComponent();
        }

        private void btnValider_Click(object sender, RoutedEventArgs e)
        {

            Control monControl = ValidateFormulaire();
            if (monControl == null)
            {

                Clients monClient = new Clients();

                monClient.adresse           = txtAdresse.Text;
                monClient.Age               = Int32.Parse(txtAge.Text);
                monClient.DateNaiss         = dpkDateNaissance.SelectedDate;
                monClient.email             = txtEmail.Text;
                monClient.Fumeur            = rbFumeurYes.IsChecked ;
                monClient.Nom               = txtNom.Text;
                monClient.Prenom            = txtPrenom.Text;
                monClient.telephone_Bureau  = txtTelBureau.Text;
                monClient.telephone_fixe    = txtTelFixe.Text;
                monClient.telephone_mobile  = txtTelMobile.Text;
                monClient.titre             = txtTitre.Text;
                monClient.ville             = txtVille.Text;

                try
                {
                    monClient.ID = srvService.AddClients(monClient);
                    MessageBox.Show("Enregistrement avec réussit !","Malaw",MessageBoxButton.OK,MessageBoxImage.Information);
                }
                catch (Exception ex)
                {

                    MessageBox.Show("Echec de l'enregistrement !\n" + ex.Message, "Malaw", MessageBoxButton.OK, MessageBoxImage.Information);

                    return;
                }

                var win = new Hotel.WPF.ui.Reservations.W_Reservation(monClient);
                win.ShowDialog();

            }
            else
            {
                MessageBox.Show("Champs obligatoire !", "Malaw", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                monControl.Focus();
            }
        }

        /// <summary>
        /// Validation du formulaire
        /// </summary>
        private Control ValidateFormulaire()
        {

            if (String.IsNullOrEmpty(txtAdresse.Text))
                return txtAdresse;

            if (String.IsNullOrEmpty(this.txtAge.Text))
                return txtAge;

            if (String.IsNullOrEmpty(this.txtNom.Text))
                return txtNom;

            if (String.IsNullOrEmpty(this.txtPrenom.Text))
                return txtPrenom;

            if (String.IsNullOrEmpty(this.txtTitre.Text))
                return txtTitre;

            if (String.IsNullOrEmpty(this.txtVille.Text))
                return txtVille;

            return null;
        }

        private void btnFermer_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void dpkDateNaissance_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            this.txtAge.Text = (System.DateTime.Now.Year - dpkDateNaissance.SelectedDate.Value.Year).ToString();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            busyTool.Width = SystemParameters.WorkArea.Width;
            busyTool.Height = SystemParameters.WorkArea.Height;

            loadListePays();
        }


        private void loadListePays()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );

                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    writeToBusyTool("Chargement de la liste des pays en cours...");
                    srvService = new HotelClient();
                    srvService.Open();
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        cboPays.DataContext = srvService.getListPays();
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Liste pays", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void writeToBusyTool(string message)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (SendOrPostCallback)delegate
                {
                    busyTool.BusyContent = message;
                },
                null
                );
        }

        private void image1_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

    }
}
